<script src="${ctx}/assets/js/jquery.validate.js"></script>
<script src="${ctx}/extra/custom/js/jquery.hiwan.js" type="text/javascript"></script>
<script src="${ctx}/js/jquery.uploadify.min.js" type="text/javascript"></script>
<fieldset>
<div class="row">
	<div class="col-xs-12">
		<form  id="dataDetailForm" name="dataDetailForm" method="post" action="${ctx}/file/upload.do">
			<input type="hidden" id="flag"   name="flag" value="${flag}"/>
			<input type="hidden" id="applyFile" name="applyFile" value="${dataInfo.applyFile}"/>
			<input type="hidden" id="busiId" name="busiId" value="${dataInfo.busiId}"/>
			<input type="hidden" id="applyState" name="applyState" value="${dataInfo.applyState}"/>
			<input type="hidden" id="applyArea" name="applyArea" value="${dataInfo.applyArea}"/>
			<input type="hidden" id="applyDept" name="applyDept" value="${dataInfo.applyDept}"/>
			<input type="hidden" id="applyEmp" name="applyEmp" value="${dataInfo.applyEmp}"/>
		    <div class="row">
		       <div class="form-group col-xs-6">
		       		<label class="col-sm-3 control-label no-padding-left" for="applyArea">申请机构：</label>
					<div class="col-sm-9">
						<input type="text" id="applyAreaName" name="applyAreaName"  value="<@libMacro.orgName orgId='${dataInfo.applyArea}' />" disabled/>
		      		</div>
		       </div>
		       <div class="form-group col-xs-6">
		       		<label class="col-sm-3 control-label no-padding-left" for="applyDept">申请部门：</label>
					<div class="col-sm-9">
						<input type="text" id="applyDeptName" name="applyDeptName"  value="${dataInfo.applyDept}" disabled/>
		            </div>
		       </div>   
			</div>
			<div class="row">
		       <div class="form-group col-xs-6">
		       		<label class="col-sm-3 control-label no-padding-left" for="applyEmp">申请人：</label>
					<div class="col-sm-9">
						<input type="text" id="applyEmpName" name="applyEmpName"  value="<@libMacro.userName userId='${dataInfo.applyEmp}' />" disabled/>
		            </div>
		       </div> 
		      <div class="form-group col-xs-6">
		       		<label class="col-sm-3 control-label no-padding-left" for="applyEmpTel">申请人电话：</label>
					<div class="col-sm-9">
						<input type="text" id="applyEmpTel" name="applyEmpTel"  value="${dataInfo.applyEmpTel}" disabled/>
		            </div>
		       </div> 
		     </div>
		     
		     <div class="row">
		       <div class="form-group col-xs-6">
		       		<label class="col-sm-3 control-label no-padding-left" for="applyDates">申请日期：</label>
					<div class="col-sm-9">
						<input type="text" id="applyDates" name="applyDate"  value="${dataInfo.applyDate}" disabled/>
		            </div>
		       </div> 
		      <div class="form-group col-xs-6">
		       		<label class="col-sm-3 control-label no-padding-left" for="dataValidity">数据有效期限：</label>
					<div class="col-sm-9">
						<input type="text" id="dataValidity" name="dataValidity"  value="${dataInfo.dataValidity}" onchange="checkDate()" disabled/>
		            </div>
		       </div> 
		     </div>
		     
		    <div class="row">
		      <div class="form-group col-xs-12">
					<label class="col-sm-12 control-label no-padding-right" for="applyCause">申请事项原因及详细要求说明(1000字符或500汉字)：<font color="red">*</font></label>
		      </div>
		    </div>
		    
		    <div class="row">
		      <div class="form-group">
					<div class="col-sm-9">
					   <div class="clearfix">
					     <textarea  id="applyCause" name="applyCause" onpropertychange="if(value.length>400) value=value.substr(0,400)" class="smallArea"  cols="80" rows="5" disabled>${dataInfo.applyCause}</textarea>
					   </div>
					</div>
		      </div>
		    </div>
		</form>	
		
		<form>
		<div class="row">
			 <div class="form-group">
                <label class="col-sm-12 control-label no-padding-left" >附件上传(限10M内，多个文件请打包后上传)：</label>
             </div>
				<div class="col-sm-9" id="td_upload">
				   <input id="my-file-input" name="fileData" type="file">			
				</div>
				<div class="hr hr-12 dotted"></div>
				
				<a id="mSubmit" type="submit" class="btn btn-sm btn-primary">Submit</a>
				<a id="mReset"  type="reset" class="btn btn-sm">Reset</a>
				
				<div class="col-sm-9" id="td_download">
				   <a id="fileDownLoad"  onclick="selectExportInfo();return false;" ></a>&nbsp;
				   <a href="#" onclick="deleteFileUpload()">【删除】</a>
				</div>
			</div>		
	    </form>	
	</div><!-- /.col -->
</div><!-- /.row -->
</fieldset>

<script type="text/javascript">
jQuery(function($) {
	initDetail();
	
	$( "#applyDates" ).datepicker({
		showOtherMonths: true,
		selectOtherMonths: false,
		dateFormat:'yy-mm-dd',
	});
	
	$( "#dataValidity" ).datepicker({
		showOtherMonths: true,
		selectOtherMonths: false,
		dateFormat:'yy-mm-dd',
	});
	
	var upload_in_progress = false;
	$('#my-file-input').ace_file_input({
		
		style : 'well',
		btn_choose : 'Select or drop files here',
		btn_change: null,
		droppable: true,
		thumbnail: 'large',		
		maxSize: 11000000,//bytes,
		before_remove: function() {
			if(upload_in_progress)
				return false;//if we are in the middle of uploading a file, don't allow resetting file input
			return true;
		},
		preview_error: function(filename , code) {
		}
	});
	
	$('#my-file-input').on('file.error.ace', function(ev, info) {
		if(info.error_count['ext'] || info.error_count['mime']) alert('Invalid file type! Please select an image!');
		if(info.error_count['size']) alert('Invalid file size! Maximum 100000KB');
	});
	
	$('#mSubmit').click(function(e) {
	     e.preventDefault();
	     uploadCommit('my-file-input','dataDetailForm');
	});

	$('#mReset').click(function() {
		$('#my-file-input').ace_file_input('reset_input_ui');
	});
	
	if(location.protocol == 'file:') alert("For uploading to server, you should access this page using 'http' protocal, i.e. via a webserver.");

 });
 
function checkDate(){
    var dataValidity = $("#dataValidity").val();
    var sysDate = new Date();
    var newDate = new Date(dataValidity.replace(/-/g,"/"));
    if(sysDate>newDate){
       alert("数据有效期限应大于当前时间");
       $("#dataValidity").val('');
       $("#dataValidity").focus();
    }
}

function save(){
		$("#dataDetailForm").validate({
		    errorElement: 'div',
		    errorClass: 'help-block',
		    focusInvalid: false,
		    rules: {
		    	applyCause: {
		          required: true
		       }
		    },		
		    messages: {
		    	applyCause: {
		           required: "请输入申请说明!"
		        } 
		    },
		
		    highlight: function (e) {
		        $(e).closest('.form-group').removeClass('has-info').addClass('has-error');
		    },
		
		    success: function (e) {
		        $(e).closest('.form-group').removeClass('has-error').addClass('has-info');
		        $(e).remove();
		    },		
		    errorPlacement: function (error, element) {
		        error.insertAfter(element.parent());
		    }
		});			
		if ($("#dataDetailForm").valid() == false) {
				return;
		} 
		
		if(!confirm("是否确定保存？")){
			return;
		}
		var url = "${ctx}/data/insert.do";
		if($("#flag").val()==1){
			url = "${ctx}/data/update.do";
		}
		saveDetail(url);	
}

function initDetail(){
	   var flag = $("#flag").val();
  	 	if(flag==1){
  	 	    disabledAll();  	 	  
  	 	    $("#btnDModify").show();
		    $("#btnDSave").hide();
		    $("#btnDCancle").hide();
		    $("#btnDClose").show();
		    $("#btnDSubmit").show();
		    $("#applyDate").attr("disabled",false);
		    $("#applyState").attr("disabled",false);
  	 	}else if(flag==0){
  	 		$("#btnDModify").hide();
  			$("#btnDSave").show();
  			$("#btnDCancle").hide();
  			$("#btnDClose").show();
  			$("#btnDSubmit").hide();
  			$("#btnDDelete").hide();
  			enabled();
  			
  	 	}
  	 	if($("#applyState").val()!=0){
  			$("#btnDDelete").hide();
  	 	}
}
   
function enabledAll(){
	   $("input").attr("disabled",false);
	   $("textarea").attr("disabled",false);
	   $("select").attr("disabled",false);
}
   
function disabledAll(){
	   $("input").attr("disabled",true);
	   $("textarea").attr("disabled",true);
	   $("select").attr("disabled",true);
}
function saveDetail(url){
	enabledAll();
	$.ajax({
       	async : false,
	   	url: url,
	   	data:$("#dataDetailForm").serialize(),
	   	type:"post",			   
	   	dataType:"json", 
	   	success: function(ret){	
	   		alert(ret[1]);
	   		$("#flag").val("1");
	   		initDetail();
		},
		error: function(ret){
		   alert("保存失败");
		}
	}); 
   }
   
function remove(){
	
	  if(!confirm("是否确定删除？")){
		return;
	  }
	    enabledAll();
		$.ajax({
	       	async : false,
		   	url: "${ctx}/data/delete.do",
		   	data:$("#dataDetailForm").serialize(),
		   	type:"post",			   
		   	dataType:"json", 
		   	success: function(ret){
		   	 if (ret[0] == "ok") {
				 alert(ret[1]);
				 $("#dataDetailIframe").dialog("close");
				 $("#dataSubListIframe").load("${ctx}/data/dataInfoList.do");
			  } else{
				  alert(ret[1]);
			  }	
			},
			error: function(ret){
				alert(ret.responseText);
			}
	}); 
}

//详细页面点击“修改”按钮时
function modify(){
	$("#btnDModify").hide();
	$("#btnDSave").show();
	$("#btnDCancle").show();
	$("#btnDClose").show();
	$("#btnDSubmit").hide();
	$("#btnDDelete").hide();
	enabled();
}

function cancle(){
	if($("#busiId").val()==""){
		var iscancle=window.confirm("确定取消？");
		if(iscancle==true){
			$('#dataDetailIframe').hide();
		}
		else{
			return;
		}
	}
	if($("#busiId").val()!=""){
		var iscancle=window.confirm("确定取消修改？");
		if(iscancle==true){
			
		}
		else{
			return;
		}
		initDetail();
		if($("#applyState").val()=="0"){
			$("#btnDDelete").show();
		}
	}	
}

function submit(){
	//流程提交
	$("#btnDSubmit").submitflow({
		busiId: '${dataInfo.busiId}',
		busiType: '0001',  
		flowType: '01',  
		isMainFlow: '1',
		applyUserId: '${sessionUserId}',
		applyUserOrg: '${sessionOrg}',
		applyUserDept: '${sessionDept}',
		reSumit: '${flowIsExist}',
		summary: 'summary',
		debug: false,		
		relativeFlowId: '',
		beforeSubmit: function() {		
	    	var ret = "0";
	    	var applyCause = $("#applyCause").val();
	    	applyCause = applyCause + "<br>"+applyCause;
	    	$("#summary").val(applyCause);
	    	if(ret=='1'){
	    		return false;
	    	}else{
	    		return true;
	    	}
	    	
		},
	 	afterSubmit: function() {  
	 		alert("流程提交成功！");
	 		$("#dataDetailIframe").dialog("close");
	 		$("#listIframe").show();
	 		$("#listIframe").load("${ctx}/data/queryDataApplyList.action?applyDate="
	                +$("#applyDate").val()+"&applyState="+$("#applyStates").val());
	 	}    	
	});

	
	
	
}

//启用
function enabled(){
		$("#applyEmpTel").attr("disabled",false);
		$("#dataValidity").attr("disabled",false);
		$("#applyCause").attr("disabled",false);
		$("#my-file-input").attr("disabled",false);
	
}
function deleteFileUpload(){
	    $("#td_upload").show();
	    $("#td_download").hide();
	    $.ajax({
	        url : "${ctx}/file/deleteFile.do",
			async : false,
			data : $("#dataDetailForm").serialize(),
			type : "POST",
			dataType : "json",
			success : function(ret) {
	    			 alert(ret[1]);
	    			 $("#applyFile").val('');
			},
			error: function(ret){
	    		   alert(ret.responseText);
	    	 }
	    });
}
   
   
function selectExportInfo(){
		var applyFile = $("#applyFile").val();
	    var applyArea = $("#applyArea").val();
	    var params = "applyFile="+applyFile+"&applyArea="+applyArea+"&fileType=1";
	    window.location = "${ctx}/file/downloadFile.do?"+params;
}
</script>